// -------------------------------------------------------------------------
// Copyright (c) 2000-2009 Ufinity. All Rights Reserved.
//
// This software is the confidential and proprietary information of
// Ufinity
//
// Original author: lxt
//
// -------------------------------------------------------------------------
// UFINITY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UFINITY SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
//
// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). UFINITY
// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
// HIGH RISK ACTIVITIES.
// -------------------------------------------------------------------------

package com.ufinity.estimation.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.ufinity.common.log.LoggerFactory;
import com.ufinity.estimation.persistence.domain.ProjectDetail;
import com.ufinity.estimation.persistence.domain.ProjectHouseStructure;
import com.ufinity.estimation.service.ProjectDetailService;
import com.ufinity.estimation.site.controller.formbean.ProjectDetailBean;

/**
 * Description of the class
 * 
 * @author lxt
 * @version 1.0
 * @since 2009-12-14
 */

public class ProjectDetailServiceImpl extends
        BaseServiceImpl<ProjectDetail, Long> implements ProjectDetailService {
    public ProjectDetailServiceImpl() {
        super(LoggerFactory.getPersistenceLog(ProjectDetailServiceImpl.class),
                ProjectDetail.class);
    }

    @Override
    public void saveOrUpdateEntity(Long projectId,
            Map<Long, ProjectDetailBean> projectDetailBeans,
            List<ProjectDetailBean> newDetailBeans) {
        String method = "saveOrUpdateEntity";
        String hql = "from ProjectHouseStructure phs where phs.projectId = ?";
        List<ProjectHouseStructure> result = getHibernateTemplate().find(hql,
                projectId);

        Map<Integer, Long> phsIds = new HashMap<Integer, Long>();
        for (ProjectHouseStructure phs : result) {
            phsIds.put(phs.getOrder(), phs.getId());
            logger.debug(method, "ProjectHouseStructure id=" + phs.getId()
                    + ", order=" + phs.getOrder());

            for (ProjectDetail pd : phs.getProjectDetails()) {
                ProjectDetailBean pdb = projectDetailBeans.get(pd.getId());

                logger.debug(method, "updatePDB = " + pdb + ", id="
                        + pd.getId());

                if (pdb != null) {
                    wrapProjectDetail(pdb, pd);
                    updateEntity(pd);
                } else {
                    deleteEntity(pd.getId());
                }
            }
        }

        for (int i = 0; newDetailBeans != null && i < newDetailBeans.size(); i++) {
            ProjectDetailBean pdb = newDetailBeans.get(i);
            if (pdb == null) {
                continue;
            }

            logger.debug(method, "ProjectDetail PhsOrder=" + pdb.getPhsOrder());
            pdb.setPhsId(phsIds.get(pdb.getPhsOrder()));
            saveEntity(wrapProjectDetail(pdb, null));
        }

    }

    private ProjectDetail wrapProjectDetail(ProjectDetailBean pdb,
            ProjectDetail pd) {
        if (pd == null) {
            pd = new ProjectDetail();
        }

        if (pdb.getAmount() != null) {
            pd.setAmount(Float.parseFloat(pdb.getAmount()));
        }
        if (pdb.getExternalPrice() != null) {
            pd.setExternalPrice(Float.parseFloat(pdb.getExternalPrice()));
        }
        if (pdb.getUnitPrice() != null) {
            pd.setUnitPrice(Float.parseFloat(pdb.getUnitPrice()));
        }
        if (pdb.getManCost() != null) {
            pd.setManCost(Float.parseFloat(pdb.getManCost()));
        }
        if (pdb.getManPrice() != null) {
            pd.setManPrice(Float.parseFloat(pdb.getManPrice()));
        }
        if (pdb.getMaterialCost() != null) {
            pd.setMaterialCost(Float.parseFloat(pdb.getMaterialCost()));
        }
        if (pdb.getMaterialPrice() != null) {
            pd.setMaterialPrice(Float.parseFloat(pdb.getMaterialPrice()));
        }

        pd.setFixedName(pdb.getFixedName());
        pd.setPhsOrder(pdb.getPhsOrder());
        pd.setUserDefined(pdb.getUserDefined());
        pd.setUnit(pdb.getUnit());
        pd.setWorkmanship(pdb.getWorkmanship());
        pd.setOrder(pdb.getOrder());
        if (pd.getPhsId() == null) {
            pd.setPhsId(pdb.getPhsId());
        }
        return pd;
    }
}
